package org.wyn.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.wyn.common.ResultType;
import org.wyn.mongodb.bean.FreshThing;
import org.wyn.mongodb.bean.Message.Mee;
import org.wyn.mongodb.bean.User;
import org.wyn.mongodb.bean.UserDetail;
import org.wyn.mongodb.dao.impl.UserDetailDao;
import org.wyn.service.IFreshThingService;
import org.wyn.service.IProfileTabService;

import com.google.inject.Inject;
/**
 * 个人主页tab 
 * @author wyn
 *
 */
@Namespace("/profile_tab")
@ParentPackage("security")
public class ProfileTabAction extends WynActionSupport{
	/**
	 * 进入个人主页tab
	 * @return
	 */
	@Action(value="go_profile_tab",results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "profile_tab.ftl")})
	public String goProfileTab(){
		User user = getLoginUser();
		List<String[]> recentVisitors = iProfileTabService.getRecentVisitors(user.getUserName());
		ud = userDetailDao.findOne("userName", user.getUserName());
		getRequest().setAttribute("recentVisitors", recentVisitors);
		return SUCCESS;
	}
	/**
	 * 查看新鲜事
	 * @return
	 */
	@Action(value="go_fresh_things",results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "fresh_things.ftl")})
	public String goFreshThings(){
		freshThingList = iFreshThingService.findByOwner(getLoginUser().getUserName());
		return SUCCESS;
	}
	/**
	 * 查看留言
	 * @return
	 */
	@Action(value="go_message",results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "message.ftl")})
	public String goMessage(){
		List<Mee> mees = iProfileTabService.viewMessageByToUser(getLoginUser().getUserName());
		getRequest().setAttribute("mees", mees);
		return SUCCESS;
	}
	/**
	 * 进入别人的个人主页tab
	 * @return
	 */
	@Action(value="go_other_profile_tab",results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "other_profile_tab.ftl")})
	public String goOtherProfileTab(){
		UserDetail otherUd = iProfileTabService.getUserDetailByUserName(otherUserName);
		getRequest().setAttribute("otherUd", otherUd);
		return SUCCESS;
	}
	/**
	 * 查看别人的新鲜事
	 * @return
	 */
	@Action(value="go_other_fresh_things",results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "other_fresh_things.ftl")})
	public String goOtherFreshThings(){
		freshThingList = iFreshThingService.findByOwner(otherUserName);
		return SUCCESS;
	}
	/**
	 * 查看别人的留言
	 * @return
	 */
	@Action(value="go_other_message",results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "other_message.ftl")})
	public String goOtherMessage(){
		List<Mee> mees = iProfileTabService.viewMessageByToUser(otherUserName);
		getRequest().setAttribute("mees", mees);
		return SUCCESS;
	}
	/**
	 * 给别人留言
	 * @return
	 */
	@Action(value="leave_mesage",results = {@Result(type = ResultType.PLAIN_TEXT)})
	public void leaveMessage(){
		User user = getLoginUser();
		iProfileTabService.leaveMessage(user.getUserName(), otherUserName, messageBody);
	}
	private UserDetail ud;
	private String messageBody;
	private String otherUserName;
	private List<FreshThing> freshThingList;
	public List<FreshThing> getFreshThingList() {
		return freshThingList;
	}
	public void setFreshThingList(List<FreshThing> freshThingList) {
		this.freshThingList = freshThingList;
	}
	@Inject
	private IProfileTabService iProfileTabService;
	@Inject
	private IFreshThingService iFreshThingService;
	@Inject
	private UserDetailDao userDetailDao;
	private static final long serialVersionUID = 1L;

	public IProfileTabService getiProfileTabService() {
		return iProfileTabService;
	}

	public void setiProfileTabService(IProfileTabService iProfileTabService) {
		this.iProfileTabService = iProfileTabService;
	}
	public String getOtherUserName() {
		return otherUserName;
	}
	public void setOtherUserName(String otherUserName) {
		this.otherUserName = otherUserName;
	}

	public String getMessageBody() {
		return messageBody;
	}

	public void setMessageBody(String messageBody) {
		this.messageBody = messageBody;
	}
	public IFreshThingService getiFreshThingService() {
		return iFreshThingService;
	}
	public void setiFreshThingService(IFreshThingService iFreshThingService) {
		this.iFreshThingService = iFreshThingService;
	}
	public UserDetailDao getUserDetailDao() {
		return userDetailDao;
	}
	public void setUserDetailDao(UserDetailDao userDetailDao) {
		this.userDetailDao = userDetailDao;
	}
	public UserDetail getUd() {
		return ud;
	}
	public void setUd(UserDetail ud) {
		this.ud = ud;
	}
}
